﻿using System.CodeDom;
using Roslyn.Compilers.CSharp;

namespace InterfaceWeaver.CustomTool
{
    public static class TypeSyntaxExtensions
    {
        /// <summary>
        /// Converts to a field name reference.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public static string CreateFieldNameReference(this TypeSyntax type)
        {
            return type.ToFullString().Trim().ToCamelCase() + "Instance";
        }

        public static CodeTypeReference ConvertTypeNameIntoTypeReference(string typeName)
        {
            switch (typeName)
            {
                case "string":
                    return new CodeTypeReference(typeof(string));
                case "int":
                    return new CodeTypeReference(typeof(int));
                case "double":
                    return new CodeTypeReference(typeof(double));
                case "float":
                    return new CodeTypeReference(typeof(float));
                case "char":
                    return new CodeTypeReference(typeof(char));
                case "byte":
                    return new CodeTypeReference(typeof(byte));
                case "sbyte":
                    return new CodeTypeReference(typeof(sbyte));
                case "decimal":
                    return new CodeTypeReference(typeof(decimal));
                case "uint":
                    return new CodeTypeReference(typeof(uint));
                case "long":
                    return new CodeTypeReference(typeof(long));
                case "ulong":
                    return new CodeTypeReference(typeof(ulong));
                case "object":
                    return new CodeTypeReference(typeof(object));
                case "short":
                    return new CodeTypeReference(typeof(short));
                case "ushort":
                    return new CodeTypeReference(typeof(ushort));
                case "bool":
                    return new CodeTypeReference(typeof(bool));
            }

            return new CodeTypeReference(typeName);
        }

        /// <summary>
        /// Converts to type reference.
        /// </summary>
        /// <param name="typeDeclaration">The type declaration.</param>
        /// <returns></returns>
        public static CodeTypeReference ToTypeReference(this TypeSyntax typeDeclaration)
        {
            var typeName = typeDeclaration.ToFullString().Trim();
            return ConvertTypeNameIntoTypeReference(typeName);
        }
    }
}
